package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.GoodsManageListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.GoodsManageListEditAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品管理-商品管理列表界面(页面一)
 * @author cyj
 */
public class GoodsManageListOperationViewModel extends BaseViewModel {

    private int page = 1;
    public GoodsManageListEditAdapter adapter;
    /** 商品类型。注意了。目前只有上架与下架，并且两种状态在操作时是相对的，即上架列表的数据只能下架。因此数据绑定里用这个字段简单判断了 */
    public ObservableField<String> status = new ObservableField<>("");
    /** 商品管理列表 */
    public MutableLiveData<List<GoodsManageListEntity>> goodsList = new MutableLiveData<>();

    public GoodsManageListOperationViewModel() {
        adapter = new GoodsManageListEditAdapter();
    }

    /**
     * 获取商品列表
     * @param isLoadMore    是否加载更多
     */
    public void getGoodsList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID,"");
        Map map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", Constants.PAGE_SIZE_10);
        map.put("status", status.get());
        map.put("companyId", companyId);
        showLoading();
        GoodsNet.getInstance().getGoodsApi().myGoodsList(map).enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<GoodsManageListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<GoodsManageListEntity>>> data) {
                goodsList.setValue(data.getData().getList());
                if(isLoadMore) {
                    adapter.addData(data.getData().getList());
                } else {
                    adapter.setNewData(data.getData().getList());
                }
            }

            @Override
            public void onFail() {
                goodsList.setValue(null);
            }
        });
    }

    /** 上、下架、删除商品 */
    public void upOrDownOrDeleteGoods(String opt) {
        Map map = new HashMap();
        map.put("status", opt);
        map.put("goodsIds", adapter.getSelectList());
        showLoading();
        GoodsNet.getInstance().getGoodsApi().upDown(map).enqueue(new GoodsCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                String msg = "";
                if (opt.equals(Constants.GOODS_OPT_UP)) {
                    msg = "上架成功";
                } else if (opt.equals(Constants.GOODS_OPT_DOWN)) {
                    msg = "下架成功";
                } else if (opt.equals(Constants.GOODS_OPT_DEL)) {
                    msg = "删除成功";
                }
                MToast.showToast(application.getApplicationContext(), msg);
                // 重置选中
                adapter.setSelectAll(false);
                getGoodsList(false);
            }
        });
    }

}
